package bairui.international.console.web.controller.delicate.category.product;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

import com.lianjing.sh.model.uml.model.status.State_UpDown;
import com.lianjing.sh.project.config.web.Project_Config_Web_PageTemplate;
import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.base.operate.OperateTarget;
import com.lianjing.sh.spring.web.controller.enhance.module.Module;
import com.lianjing.sh.spring.web.controller.enhance.module.context.ModuleContext;
import com.lianjing.sh.spring.web.controller.enhance.navigations.context.NavigationContext;

import bairui.international.model.domain.jpa.TB_Product;
import bairui.international.model.domain.jpa.TB_ProductProperty;
import bairui.international.model.domain.jpa.TB_ProductPropertyItem;
import bairui.international.model.type.Type_ProductProperty;
import bairui.international.service.Service_Product;
import bairui.international.service.Service_ProductProperty;
import bairui.international.service.Service_ProductPropertyItem;

/**
 *
 * 产品属性编辑
 *
 * @generated
 * 
 * @see STest_C_Delicate_Category_ProductPropertyEdit_EditAccess 测试
 *
 */
@Controller
public class C_Delicate_Category_ProductPropertyEdit_EditAccess
 extends SC_Delicate_Category_ProductPropertyEdit_EditAccess 
{
	
	@Autowired private Service_ProductPropertyItem service_ProductPropertyItem;
	@Autowired private Service_ProductProperty service_ProductProperty;
	@Autowired private Service_Product service_Product;
	
	/**
	 * 访问编辑页面
	 */
	@Action(EditAccess.class)
	@GetMapping(EditAccess.RequestMappingValue)
	public ModelAndView editAccess(
		ModelMap map, HttpServletRequest request,
		NavigationContext navigationContext, ModuleContext moduleContext, 
		@PathVariable(name=EditAccess.PathVariableName.CategoryId) long categoryId, 
		@PathVariable(name=EditAccess.PathVariableName.Id) long id
	) throws Exception {
		TB_Product bean = service_Product.findById(id);
		List<TB_ProductProperty> ps = service_ProductProperty.list(it->it.productCategoryProperties.any().productCategory.id.eq(categoryId).and(it.state.eq(State_UpDown.Uped)));
		List<Module> modules = new ArrayList<>();
		//
		moduleContext
			.addModule(this.module_Fun.module(new C_Delicate_Category_ProductDetailEdit_EditAccess.EditAccess.Operate(){
				@Override public Object getCategoryId() { return categoryId; }
				@Override public Object getId() { return id; }
				@Override public OperateTarget target() {
					return OperateTarget.Self;
				}
			}));
		for(TB_ProductProperty property : ps){
			if(property.getType()== Type_ProductProperty.Rang){
				continue;
			}
			TB_ProductPropertyItem pItem = service_ProductPropertyItem.findOne(it->it.productProperty.eq(property).and(it.products.any().eq(bean)));
			//
			modules.add(module_ProductPropertyEdit.module(
							new Module_ProductPropertyEdit.Model(bean, property.getName(), pItem),
							new C_Delicate_Category_ProductPropertyEdit_Edit.Edit.Operate(){
								@Override public Object getCategoryId() { return categoryId; }
								@Override public Object getId() { return id; }
								public com.lianjing.sh.spring.web.controller.base.operate.OperateSupport returnTo() {
									return new C_Delicate_Category_ProductPropertyEdit_EditAccess.EditAccess.Operate() {
										@Override public Object getId() {
											return id;
										}
										@Override public Object getCategoryId() {
											return categoryId;
										}
									};
								};
							},
							service_ProductPropertyItem.list(it->it.productProperty.eq(property).and(it.state.eq(State_UpDown.Uped)))
						));
		}
		/* 模块 */
		moduleContext
			.addModule(modules.toArray(new Module[]{}));
		return Project_Config_Web_PageTemplate.Admin_Common(request);
	}
	

}
